package org.example;

import java.util.Stack;

public class Test4 {
    /**
     * 方法一：栈
     * @param s
     * @return
     */
    public String removeDuplicates(String s) {
        Stack<Character> stack  = new Stack<>();
        for (char c : s.toCharArray()) {
            if (!stack.empty()) {
                char t = stack.peek();
                if (t == c) stack.pop();
                else stack.add(c);
            }else {
                stack.add(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.empty()) sb.append(stack.pop());
        return sb.reverse().toString();
    }

    /**
     * StringBuilder动态修改字符串内容
     * @param s
     * @return
     */
    public String removeDuplicates1(String s) {
        StringBuilder sb = new StringBuilder();
        int len = 0;
        for (char c : s.toCharArray()) {
            len = sb.length();
            if (len > 0) {
                if (c == sb.charAt(len - 1)) sb.deleteCharAt(len - 1);
                else sb.append(c);
            }else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        Test4 test4 = new Test4();
        test4.removeDuplicates("abbaca");
        System.out.println(666);
    }
}
